import { BattleCard, Skill } from '../types';
export const monster = (
  card: BattleCard,
  skl: Skill,
  own: BattleCard[],
  foe: BattleCard[],
): BattleCard[] => {
  const { from, base } = card;
  const { position } = base;
  const { action, limits, area } = skl;
  let cards: BattleCard[] = [];

  switch (action) {
    case 1:
      cards = from === 'OWN' ? foe : own;
      break;
    case 2:
      cards = from === 'OWN' ? own : foe;
      break;
    case 3:
      cards = from === 'OWN' ? foe : own;
      break;
    case 4:
      cards = from === 'OWN' ? own : foe;
      break;
    default:
      break;
  }

  let result: BattleCard[] = [];

  switch (limits) {
    case 1:
      result = col(cards, area, position);
      break;
    case 2:
      result = row(cards, area, position);
      break;
    case 3:
      result = row(cards, -area, position);
      break;
    case 4:
      result = hp(cards, area);
      break;
    case 5:
      result = [card];
      break;
    case 7:
      result = ten(cards);
      break;
  }
  return result;
};
/**
 * 横排寻怪
 * @param {BattleCard[]} cards 卡牌
 * @param {number} area 区域 1 | 3 ｜6 ｜9
 * @returns
 */
export const col = (
  cards: BattleCard[],
  area: number,
  position?: [number, number],
): BattleCard[] => {
  let result: BattleCard[] = [];
  switch (area) {
    case 1:
      result = row(cards, 1, position ? position : [1, 1]);
      break;
    case 3:
      result = cards.filter(v => v.base.position[1] === 1);
      break;
    case 6:
      result = cards.filter(v => v.base.position[1] === 1 || v.base.position[1] === 2);
      break;
    case 9:
      result = cards;
      break;
    default:
      break;
  }
  return result;
};

/**
 * 直线寻怪
 * @param {BattleCard[]} cards 卡牌
 * @param {number} Area 区域 1 ｜ 3
 * @param {number} Position 位置
 * @returns
 */
export const row = (
  cards: BattleCard[],
  area: number,
  position: [number, number],
): BattleCard[] => {
  let result: BattleCard[] = [];

  const x = position[0];

  result = cards.filter(v => v.base.position[0] === x);

  if (result.length > 0) {
    return area > 0 ? result.slice(0, area) : result.slice(area);
  }

  for (let i = 1; i <= 3; i++) {
    result = cards.filter(v => v.base.position[0] === i);
    if (result.length > 0) {
      break;
    }
  }

  return area > 0 ? result.slice(0, area) : result.slice(area);
};

/**
 * 血量寻怪
 * @param {BattleCard[]} cards 卡牌
 * @param {number} Area 区域 1 ｜ 9
 */
export const hp = (cards: BattleCard[], area: number): BattleCard[] => {
  const cCards = [...cards];
  const scCards = cCards.sort((a: BattleCard, b: BattleCard) => {
    return a.battle.HP - a.base.hp - (b.battle.HP - b.base.hp);
  });

  let result: BattleCard[] = [];

  scCards.forEach(v => {
    result.push(cards.find(c => c.base.id === v.base.id) as BattleCard);
  });

  return result.splice(0, area);
};

/**
 * 十字寻怪
 * @param {BattleCard[]} cards 卡牌
 */
export const ten = (cards: BattleCard[]): BattleCard[] => {
  let result: BattleCard[] = [];
  const positions: string[] = ['12', '21', '22', '23', '32'];
  console.log(cards);
  cards.forEach(v => {
    console.log(v.base.position);
    console.log(positions.includes(`${v.base.position[0]}${v.base.position[1]}`));

    if (positions.includes(`${v.base.position[0]}${v.base.position[1]}`)) {
      result.push(v);
    }
  });
  return result;
};
